<html>
<?php
$oa = $_GET['oa'];
$da = $_GET['da'];
?>
<script language="javascript" type="text/javascript">
<!--
var currentRow = 0;

function newRequest() {
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try {
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer Browsers
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// Something went wrong
				alert("Your browser broke!");
				
				return false;
			}
		}
	}
	return ajaxRequest;
}

//Browser Support Code
function getAirportData(icao, field) {
	ajaxRequest = newRequest();	
	ajaxRequest.onreadystatechange = function() {
		if (ajaxRequest.readyState == 4) {
			// get the data from the server's response	
				data = ajaxRequest.responseText.split(":");
				//data[6] = Math.round(data[6]*100)/100;
			document.getElementById(field + 'name').innerHTML = data[2];
			document.getElementById(field + 'lat').innerHTML = Math.round(100*data[3])/100 + "&deg;";
			document.getElementById(field + 'long').innerHTML = Math.round(100*data[4])/100 + "&deg;";
			document.getElementById(field + 'elev').innerHTML = data[5] + " ft";
			document.getElementById(field + 'mag_var').innerHTML = (data[6] > 0) ? (data[6] + "&deg; E") : (-1*data[6] + "&deg; W");
			document.getElementById(field + 'tz').innerHTML = "GMT" + data[7];
			document.getElementById(field + 'metar').innerHTML = data[8]; 
		}
	}
	ajaxRequest.open("GET", "getdata.php?icao=" + icao, true);
	ajaxRequest.send();
}

function getRouteData() {	
	icao1 = document.getElementById('oa').value;
	icao2 = document.getElementById('da').value;
	if (icao1 != "" && icao2 != "") {
		ajaxRequest = newRequest();
		ajaxRequest.onreadystatechange = function() {
			if (ajaxRequest.readyState == 4) {
				// get the data from the server's response	
				data = ajaxRequest.responseText.split(":");
				document.getElementById('ft' + currentRow).innerHTML = icao1 + " -> " + icao2;
				document.getElementById('d' + currentRow).innerHTML = Math.round(data[0]);
				//document.getElementById('tb' + currentRow).innerHTML = data[1] + "&deg;";
				document.getElementById('mb' + currentRow).innerHTML = data[2] + "&deg;";		
			}
		}
		ajaxRequest.open("GET", "getdata.php?icao1=" + icao1 + "&icao2=" + icao2, true);
		ajaxRequest.send();
	}
}

function addRow(tableID) {
		var table = document.getElementById(tableID);
		var rowCount = table.rows.length;
		var row = table.insertRow(rowCount);
		
		var cellAdd = row.insertCell(0);
		var buttonAdd = document.createElement("input");
		buttonAdd.type = "button";
		buttonAdd.value = "+";
		buttonAdd.onClick = "getRouteData();addRow(\'flightPlan\')";

		var cellICAO = row.insertCell(1);
		cellICAO.innerHTML = "<div id=\"ft" + rowCount + "\"></div>";
		
		var cellMOCA = row.insertCell(2);
		var textBoxMOCA = document.createElement("input");
		textBoxMOCA.type = "text";
		textBoxMOCA.setAttribute('size', 5);
		textBoxMOCA.setAttribute('maxlength', 5);
		cellMOCA.appendChild(textBoxMOCA);

		var cellAlt = row.insertCell(3);
		var textBoxAlt = document.createElement("input");
		textBoxAlt.type = "text";
		textBoxAlt.setAttribute('size', 5);
		textBoxAlt.setAttribute('maxlength', 5);
		cellAlt.appendChild(textBoxAlt);

		var cellRPM = row.insertCell(4);
		var textBoxRPM = document.createElement("input");
		textBoxRPM.type = "text";
		textBoxRPM.setAttribute('size', 4);
		textBoxRPM.setAttribute('maxlength', 4);
		cellRPM.appendChild(textBoxRPM);

		var cellIAS = row.insertCell(5);
		var textBoxIAS = document.createElement("input");
		textBoxIAS.type = "text";
		textBoxIAS.setAttribute('size', 3);
		textBoxIAS.setAttribute('maxlength', 3);
		cellIAS.appendChild(textBoxIAS);

		var cellMB = row.insertCell(6);
		cellMB.innerHTML = "<div id=\"mb" + rowCount + "\"></div>";

		var cellD = row.insertCell(7);
		cellD.innerHTML = "<div id=\"d" + rowCount + "\"></div>";

		var cellGS = row.insertCell(8);
		cellGS.innerHTML = "<div id=\"gs" + rowCount + "\"></div>";

		var cellETE = row.insertCell(9);
		cellETE.innerHTML = "<div id=\"ete" + rowCount + "\"></div>";

		var cellETA = row.insertCell(10);
		cellETA.innerHTML = "<div id=\"eta" + rowCount  + "\"></div>";
		
		currentRow++;
}
/*
function addRow() {
	echo "<tr>";
	echo "<td><input type=\"button\" name=\"addRow\" value=\"+\" onClick=\"getRouteData()\"></td>";
	echo "<td><div id=\"ft\"></div></td>";
	echo "<td><input type=\"text\" name=\"moca\" maxlength=5 size=5></td>";
	echo "<td><input type=\"text\" name=\"alt\" maxlength=5 size=5></td>";
	echo "<td><input type=\"text\" name=\"rpm\" maxlength=4 size=4></td>";
	echo "<td><input type=\"text\" name=\"ias\" maxlength=3 size=3></td>";
	echo "<td><div id=\"mb\"></div></td>"
	echo "<td><div id=\"d\"></div></td>"
	echo "<td><div id=\"gs\"></div></td>"
	echo "<td><div id=\"ete\"></div></td>"
	echo "<td><div id=\"eta\"></div></td>"
	echo "</tr>"
}
 */

-->

</script>
<table border="1" cellspacing="1" cellpadding="2">
<tr>
<th></th>
<th>ICAO</th>
<th>Name</th>
<th>Lat</th>
<th>Long</th>
<th>Elev</th>
<th>Mag Var</th>
<th>TZ</th>
<th>METAR</th>
</tr>
<tr>
<td><font face="Arial,Helvetica,sans-serif">Origin Airport</font></td>
<td><input type="text" name ="oa" maxlength=4 size=4 id="oa" value="<?php echo $oa ?>" onChange="getAirportData(this.value, this.name)"></td>
<td><div id="oaname"></div></td>
<td><div id="oalat"></div></td>
<td><div id="oalong"></div></td>
<td><div id="oaelev"></div></td>
<td><div id="oamag_var"></div></td>
<td><div id="oatz"></div></td>
<td><div id="oametar"></div></td>
</tr>
<tr>
<td><font face="Arial,Helvetica,sans-serif">Destination Airport</font></td>
<td><input type="text" name="da" maxlength=4 size=4 id="da" value="<?php echo $da ?>" onChange="getAirportData(this.value, this.name)"></td>
<td><div id="daname"></div></td>
<td><div id="dalat"></div></td>
<td><div id="dalong"></div></td>
<td><div id="daelev"></div></td>
<td><div id="damag_var"></div></td>
<td><div id="datz"></div></td>
<td><div id="dametar"></div></td>
</tr>
</table>
<br/><br/>
<table id="flightPlan" border="1" cellspacing="1" cellpadding="2">
<tr>
<td><input type="button" name="addRow" value="+" onClick="getRouteData(); addRow('flightPlan')"></td>
<th>From-To</th>
<th>MOCA</th>
<th>Alt.</th>
<th>RPM</th>
<th>IAS</th>
<th>Comp.<br/>Hdg.</th>
<th>Dist.</th>
<th>G/S</th>
<th>ETE</th>
<th>ETA</th>
</tr>
<!--<tr>
<td><input type="button" name="addRow" value="+" onClick="getRouteData(); addRow('flightPlan')"></td>
<td><div id="ft1"></div></td>
<td><input type="text" name="moca1" maxlength=5 size=5></td>
<td><input type="text" name="alt1" maxlength=5 size=5></td>
<td><input type="text" name="rpm1" maxlength=4 size=4></td>
<td><input type="text" name="ias1" maxlength=3 size=3></td>
<td><div id="mb1"></div></td>
<td><div id="d1"></div></td>
<td><div id="gs1"></div></td>
<td><div id="ete1"></div></td>
<td><div id="eta1"></div></td>
</tr>-->
</table>
<?php


?>
</html>
